window.onload = init;

function init() {
    sessionName();

    searchOnClick();
}

//登录入口
function sessionName(){
    var username = $("#loginUser").text();
    if(username){
        document.getElementById("sessionName").innerText = "你好，" + username;
        document.getElementById("sessionName").href = "jump?pg=reception/baseInfoSet";
    } else {
        document.getElementById("sessionName").innerText = "你好，请登录";
    }
}

//弹出登录提示
$(function(){
    $("#shoppingCar").on("click",function(){
        var username = $("#loginUser").text();
        if(username == ''){
            alert("请先登录！！！");
        } else {
            $(this).attr("href","jump?pg=reception/shoppingCar");
        }
    });
});

function searchOnClick() {
    let searchValue = $("#searchValue").val();
    $.ajax({
        type: "get",
        url: "queryGoodsByName",
        data: "searchValue="+searchValue,
        success: function (data) {
            if (data) {
                // 将查询的数据源进行假分页处理
                $("#queryPager").pagination({
                    dataSource: data,
                    pageSize: 20,
                    showGoInput: true,
                    showGoButton: true,
                    callback: function (pageData, pagination) {
                        // template method of yourself
                        var strMsg = "";
                        // 使用each循环遍历，json数组中的每个对象，并将每个对象的属性值取出进行显示
                        $.each(pageData, function (index, eachGoods) {
                            strMsg += "<div class='inner'>";
                            strMsg += "<div onclick='productDetails(\""+eachGoods.gid+"\")'>";
                            strMsg += "<img src='" + eachGoods.image + "' alt='hahaha' width='80%' height='80%' /><br/>";
                            strMsg += "<span>"+eachGoods.gname+"</span><br/>";
                            strMsg += "<span>"+eachGoods.price+"￥/"+eachGoods.unit+"</span><br/>";
                            strMsg += "</div>";
                            strMsg += "<span onclick='addToCart(\""+eachGoods.gid+"\")'>加入购物车</span>";
                            strMsg += "</div>";
                        });
                        $("#showMsg").html(strMsg); // 局部刷新
                    }
                });
            }
        },
        error: function () {
            alert("出错啦");
        }
    });
    $("#showMsg")
}

function searchOnKeyDown(event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 13) { // enter 键
        searchOnClick();
    }
}

function productDetails(gid) {
    alert(gid);
}

function addToCart(gid) {
    var uid = $("#loginUserId").text();
    if(uid == ''){
        alert("请先登录！！！");
    } else {
        $.ajax({
            type: "POST",
            url: "ajaxAddShoppingCart",
            data: "gid="+gid+"&uid="+uid+"&num=1",
            success: function (data) {
                if (data && data == 'ok') {
                    alert("加入购物车成功！");
                } else {
                    alert("加入购物车失败！")
                }
            },
            error: function () {
                alert("出错啦");
            }
        });
    }
}